home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / QuickDraw / Rubber Bandit / Source / RubberBand.c
Encoding:
C/C++ Source or Header  |  1996-09-17  |  4.1 KB  |  183 lines  |  [TEXT/CWIE]

  1.  
  2. /* =============================
  3.    Rubber Bandit is a simple
  4.    srcXor rubber-banding example.
  5.  
  6.    Think C 4.0.4 source.  
  7.   
  8.    - DMH, MacDTS, 3/5/91
  9.    =============================  */
  10.    
  11. #include <Dialogs.h>
  12. #include <Fonts.h>   
  13. #include <Processes.h>
  14.  
  15. /* constants:             */
  16.  
  17. #define testDLOG    1234
  18. #define OK_button    1
  19.  
  20. /* function prototypes: */
  21.  
  22. extern void HandleDLOG(DialogPtr theDialog);
  23. extern pascal Boolean RubberProc(DialogPtr theDialog, EventRecord *theEvent, short *itemHit);
  24. extern void RubberBandIt(Point anchorPt);
  25. extern Rect *CheckRect(Rect *theRect);
  26. extern void DrawStuff(Rect *rubberRect);
  27. extern void    FanStuff(Rect *theRect);
  28.  
  29.  
  30. void main(void) 
  31. {
  32.     DialogPtr    theDialog = NULL;
  33.  
  34.     InitGraf(&qd.thePort);
  35.     InitFonts();
  36.     FlushEvents(everyEvent, 0);
  37.     InitWindows();
  38.     InitMenus();
  39.     TEInit();
  40.     InitDialogs(0L);
  41.     InitCursor();
  42.     
  43.     theDialog = GetNewDialog(testDLOG, NULL, (WindowPtr) -1L);
  44.     if (theDialog != NULL) HandleDLOG(theDialog);
  45.         
  46.     ExitToShell();
  47. }
  48.  
  49.  
  50. /*    ================================================
  51.     HandleDLOG calls ModalDialog and rubber-bands
  52.     until the OK button is pressed.
  53.     ================================================    */
  54.  
  55. void HandleDLOG(theDialog)
  56. DialogPtr theDialog;
  57. {
  58.     short        itemHit;
  59.     GrafPtr        oldPort;
  60.     Point        anchorPt;
  61.  
  62.     do
  63.     {
  64.         ModalDialog(NULL, &itemHit);
  65.  
  66.         if (itemHit != OK_button)
  67.         {
  68.             GetPort(&oldPort);            /* Save the port, set it to our dialog, */
  69.             SetPort(theDialog);
  70.             GetMouse(&anchorPt);        /* get the current mouse pos. and        */
  71.             RubberBandIt(anchorPt);        /* rubber-band the image.                */
  72.             SetPort(oldPort);            /* Restore the original port.            */
  73.         }
  74.     }
  75.     while (itemHit != OK_button);
  76.     
  77.     DisposeDialog(theDialog);            /* Throw away the dialog.                */
  78. }
  79.  
  80.  
  81. /*    ================================================
  82.     RubberBandIt follows the mouse and rubber-bands
  83.     a design based on its position.  It retains
  84.     control until the mouse button is released.
  85.     ================================================    */
  86.  
  87. void RubberBandIt(anchorPt)
  88. Point    anchorPt;
  89. {
  90.     Point    curMousePt = {0, 0};
  91.     Rect    rubberRect;
  92.     Pattern gray;
  93.  
  94.     PenMode(srcXor);                                /* Set pen mode to exclusive or.*/
  95.     PenPat(&gray);
  96.  
  97.     GetMouse(&curMousePt);                            /* Get current mouse pos.        */
  98.  
  99.     rubberRect.top = anchorPt.v;                    /* Draw initial rectangle.        */
  100.     rubberRect.left = anchorPt.h;
  101.     rubberRect.bottom = curMousePt.v;
  102.     rubberRect.right = curMousePt.h;
  103.     DrawStuff(&rubberRect);
  104.  
  105.     while (Button())
  106.     {
  107.         GetMouse(&curMousePt);                        /* Get current mouse pos…        */
  108.  
  109.         if (curMousePt.h != rubberRect.right ||        /* If mouse moved…                */
  110.             curMousePt.v != rubberRect.bottom)
  111.         {
  112.             DrawStuff(&rubberRect);                    /* Erase old…                    */
  113.             rubberRect.bottom = curMousePt.v;
  114.             rubberRect.right = curMousePt.h;
  115.             DrawStuff(&rubberRect);                    /* and draw new.                */
  116.         }
  117.     }
  118.     
  119.     DrawStuff(&rubberRect);                            /* Erase old at end.            */
  120. }
  121.  
  122.  
  123. /*    ================================================
  124.     DrawStuff draws designs in the rectangle
  125.     passed, using the current pen mode, etc.
  126.     ================================================    */
  127.  
  128. void DrawStuff(rubberRect)
  129. Rect    *rubberRect;
  130. {
  131.     Rect    curRect;
  132.     
  133.     curRect = *rubberRect;
  134.     FrameRect(CheckRect(&curRect));
  135.     FanStuff(CheckRect(&curRect));
  136. }
  137.  
  138.  
  139. /*    ================================================
  140.     FanStuff draws a fanlike thing in the rect
  141.     passed, using the current pen mode, etc.
  142.     ================================================    */
  143.  
  144. void    FanStuff(theRect)
  145. Rect    *theRect;
  146. {
  147.     short    ang;
  148.  
  149.     for (ang = 0; ang < 360; ang += 60)
  150.         PaintArc(theRect, ang, 45);
  151. }
  152.  
  153.  
  154. /*    ================================================
  155.     CheckRect makes sure that the top of the
  156.     rectangle passed is ≤ the bottom and the left is
  157.     ≤ the right.  FrameRect needs things this way.
  158.     ================================================    */
  159.  
  160. Rect *CheckRect(theRect)
  161. Rect    *theRect;
  162. {
  163.     short    temp;
  164.  
  165.     if (theRect->top > theRect->bottom)        /* Need to reverse top and bottom? */
  166.     {
  167.         temp = theRect->top;
  168.         theRect->top = theRect->bottom;
  169.         theRect->bottom = temp;
  170.     }
  171.  
  172.     if (theRect->left > theRect->right)        /* Need to reverse left and right? */
  173.     {
  174.         temp = theRect->left;
  175.         theRect->left = theRect->right;
  176.         theRect->right = temp;
  177.     }
  178.     
  179.     return theRect;                            /* This makes nested calls easier.    */
  180. }
  181.  
  182.  
  183.